package ltd.sshine.bot.stroge;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class UserUtil {
  static ObjectMapper objectMapper=new ObjectMapper();
  static Map<Long,Long> user=new HashMap<>();
  static Properties properties=new Properties();
   static {

      ApplicationHome home = new ApplicationHome(UserUtil.class);
      File file=new File(home.getDir(),"app.properties");
//       System.out.println(file);
      if (file.exists()){
         try {
            properties.load(new FileInputStream(file));
            String u= (String) properties.get("user");
           user= objectMapper.readValue(u, new TypeReference<>() {
           });
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      try {
         properties.setProperty("user",objectMapper.writeValueAsString(user));
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
   }

 public static Long getUser(Long qq){
      return user.get(qq);
   }

 public static void SerUser(Long qq,Long userId){
      user.put(qq,userId);
      try {
         properties.setProperty("user",objectMapper.writeValueAsString(user));
          ApplicationHome home = new ApplicationHome(UserUtil.class);
          File file=new File(home.getDir(),"app.properties");
         properties.store(new FileOutputStream(file),"user");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }




}
